import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    category:[],
    listItems:[],
    allDetails:[],
    details:JSON.parse(localStorage.getItem('details')) || {},
    rightDetails:{},
    cartItems:[],
    totalPrice:0
  },
  mutations: {
    // 菜单数据
    GET_CATEGORY(state,items){
      // console.log(state,items)
      state.category = items
    },
    // 列表数据
    GET_LIST_ITEMS(state,list){
      // console.log(state,list)
      state.listItems = list
    },
    // 所有详情
    GET_ALL_DETAILS(state,items){
      state.allDetails = items
    },
    GET_DETAILS(state,item){
      state.allDetails.forEach((ele)=>{
        if(ele.data.mtWmPoiId === item.mtWmPoiId){
          state.details = ele
          // console.log(12312313,ele)
          localStorage.setItem('details',JSON.stringify(state.details))
        }
      })
    },
    // 右侧详情数据
    GET_DETAILS_RIGHT(state,index){
      // console.log(state,index)
      console.log(111,state.details.data.categoryList[index])
      state.details.data.categoryList[index].spuList.forEach((ele)=>{
          ele.count = 0
      })
      state.rightDetails = state.details.data.categoryList[index]
    },
    ADD_CART(state,item){
      var flag = false
      state.cartItems.forEach((ele)=>{
        if(ele.spuId === item.spuId){
          ele.count++
          flag = true
        }
      })
      if(!flag){
        item.count = 1
        state.cartItems.push(item)
      }
      state.cartItems = [...state.cartItems]

      // 总价计算
      state.totalPrice = 0
      state.cartItems.forEach((ele)=>{
        state.totalPrice += ele.count * ele.currentPrice
      })

    },
    MINUS_CART_COUNT(state,item){
      state.cartItems.forEach((ele)=>{
        if(ele.spuId === item.spuId){
          if(ele.count>0){
            ele.count--
          }
        }
      })
      state.cartItems = [...state.cartItems]

      // 总价计算
      state.totalPrice = 0
      state.cartItems.forEach((ele)=>{
        state.totalPrice += ele.count * ele.currentPrice
      })
    }
  },
  actions: {
    // 获取分类数据
    GET_CATEGORY(context) {
      axios.get('http://localhost:8080/category.json').then((res) => {
        // console.log(res.data.data.kingkongList)
        context.commit('GET_CATEGORY',res.data.data.kingkongList)
      }).catch((error) => {
        console.log(error)
      })
    },
    // 获取列表数据
    GET_LIST_ITEMS(context){
      axios.get('http://localhost:8080/listData.json').then((res) => {
        // console.log(res.data.data.shopList)
        context.commit('GET_LIST_ITEMS',res.data.data.shopList)
      }).catch((error) => {
        console.log(error)
      })
    },
    // 获取所有详情页数据
    GET_ALL_DETAILS(context){
      axios.get('http://localhost:8080/shopdetails.json').then((res) => {
        console.log(res.data)
        context.commit('GET_ALL_DETAILS',res.data)
      }).catch((error) => {
        console.log(error)
      })
    },
    // 获取点击商品的详情
    GET_DETAILS(context,item){
      context.commit('GET_DETAILS',item)
    },
    // 右侧详情数据
    GET_DETAILS_RIGHT(context,index){
      context.commit('GET_DETAILS_RIGHT',index)
    },
    ADD_CART(context,item){
      context.commit('ADD_CART',item)
    },
    MINUS_CART_COUNT(context,item){
      context.commit('MINUS_CART_COUNT',item)
    }
  },
  modules: {
  }
})
